package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /*
     * 新增菜品和对应口味
     * @param DishDto
     * @return
     * */
    @Override
    @Transactional  //开启事务，因为有两条sql 要么全成功 要么全失败
    public void save(DishDTO dto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto, dish);

//        向菜品表插入一条数据
        dishMapper.insert(dish);

//        获取insert主键生成的主键值  需要在xml sql中开启  useGeneratedKeys="true" keyProperty="id"
        Long dishId = dish.getId();

//        向口味表插入n条数据
        List<DishFlavor> flavors = dto.getFlavors();
        if (flavors != null && flavors.size() > 0) {
//            给口味赋上菜品id
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishId));
//        向口味表插入n条数据
            dishFlavorMapper.insertBatch(flavors);
        }
    }


    /*
     * 菜品分页查询
     * @param dishPageQueryDTO
     * */
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        //        调用分页
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        //        调用sql
        Page<Dish> result = dishMapper.pageQuery(dishPageQueryDTO);

        PageResult pageResult = new PageResult(result.getTotal(), result.getResult());
        return pageResult;
    }

    /*
     * 根据id查询菜品
     * @param id
     * */
    @Override
    @Transactional
    public DishVO getById(Long id) {
//        查询菜品
        Dish dish = dishMapper.getById(id);
//        查询口味
        List<DishFlavor> dishFlavor= dishFlavorMapper.getByDishId(id);

        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavor);
        return dishVO;
    }


    /*
     * 修改菜品
     * @param Dish
     * */
    @Override
    @Transactional
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
//        修改菜品
        dishMapper.update(dish);


//        修改口味
//        获取insert主键生成的主键值  需要在xml sql中开启  useGeneratedKeys="true" keyProperty="id"
        Long dishId = dish.getId();

//        先把该菜品下的口味删除
        dishFlavorMapper.delete(dishId);

//        向口味表插入n条数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
//            给口味赋上菜品id
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishId));
//        向口味表插入n条数据
            dishFlavorMapper.insertBatch(flavors);
        }
    }


    /*
    修改菜品状态
    @param status
    @param id
*/
    @Override
    public void startOrStop(Integer status, Long id) {
        Dish dish = Dish.builder().status(status).id(id).build();
        dishMapper.update(dish);
    }


    /*
     * 批量删除菜品
     * @param ids
     * @return
     * */
    @Override
    @Transactional
    public void deletes(List<Long> ids) {
//        查询菜品是否关联套餐
        List<SetmealDish> setmealDishes = setmealDishMapper.selectByDishIds(ids);
        Boolean setmealStatus = false;
        if(setmealDishes != null && setmealDishes.size()>0){
            setmealStatus = true;
        }
//        查询菜品是否处于停售状态
        List<Dish> dishList = dishMapper.selectBatch(ids);
        // 如果有任何一个菜品的 status == 1，dishStatus 就为 false
        Boolean dishStatus = dishList.stream().anyMatch(dish -> dish.getStatus() == 1);

//        都为false时，删除菜品 以及对应的口味数据
        if(!setmealStatus && !dishStatus){
            dishMapper.deleteBatch(ids);
            dishFlavorMapper.deleteBatch(ids);
        }
    }



    /*
     * 根据套餐id查询菜品
     * */
    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }



}
